﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace TestGenerator.Input
{
    public class TestResource
    {
        private static Initialize initialize;
        private static string questionTag;
        private static string correctAnswerTag;
        private static string wrongAnswerTag;
        private static string closingTag;
        private static string tableStartTag;

        private string heather;
        private string footer;
        private List<Question> questions;

        static TestResource()
        {
            initialize = new Initialize();
            questionTag = "<p class=Question>";
            correctAnswerTag = "<p class=CorrectAnswer>";
            wrongAnswerTag = "<p class=WrongAnswer>";
            closingTag = "</p>";
            tableStartTag = "<table ";
        }

        public TestResource()
        {
            string resourseText = ReadResourseFile();
            ExtractHeather(resourseText);
           // RemoveTableFromHeather();
            ExtractFooter(resourseText);
            string clearedText = ClearHeatherAndFooter(resourseText);
            string[] questionInfos = ExtractQuestionInfos(clearedText);
            questions = new List<Question>();
            foreach (string info in questionInfos)
            {
                if (info.Contains(closingTag))
                {
                    questions.Add(ExtractQuestion(info));
                }
            }
        }

        public static Initialize Initialize
        {
            get
            {
                return initialize;
            }
        }

        public string Heather
        {
            get
            {
                return heather;
            }
        }

        public string Footer
        {
            get
            {
                return footer;
            }
        }

        public List<Question> Questions
        {
            get
            {
                return questions;
            }
        }

        private void ExtractHeather(string text)
        {
            int heatherEnd = text.IndexOf(questionTag);
            heather = text.Substring(0, heatherEnd);
        }

        private void ExtractFooter(string text)
        {
            int lastIndexOfCorrectAnswer = text.LastIndexOf(correctAnswerTag);
            int lastIndexOfWrongAnswer = text.LastIndexOf(wrongAnswerTag);
            int lastIndexOfAnswer = Math.Max(lastIndexOfCorrectAnswer, lastIndexOfWrongAnswer);
            int footerStart = text.IndexOf(closingTag, lastIndexOfAnswer) + closingTag.Length;
            footer = text.Substring(footerStart);
        }

        private string ClearHeatherAndFooter(string text)
        {
            string clearedText = text.Substring(heather.Length - 1, text.Length - heather.Length - footer.Length);
            return clearedText;
        }

        private string ReadResourseFile()
        {
            string result;
            using (StreamReader reader = new StreamReader(initialize.InputFileName,Encoding.GetEncoding("windows-1251")))
            {
                result = reader.ReadToEnd(); 
            }
            return result;
        }

        private string[] ExtractQuestionInfos(string text)
        {
            string[] result = text.Split(new String[]{questionTag}, StringSplitOptions.RemoveEmptyEntries);
            return result;
        }

        private Question ExtractQuestion(string text)
        {
            string[] parameters = text.Split(new String[] { closingTag }, StringSplitOptions.RemoveEmptyEntries);
            string questionText = parameters[0];
            List<string> correct = new List<string>();
            List<string> wrong = new List<string>();
            for (int parameter = 1; parameter < parameters.Length; parameter++)
            {
                if (parameters[parameter].Contains(correctAnswerTag))
                {
                    correct.Add(parameters[parameter].Remove(parameters[parameter].IndexOf(correctAnswerTag), correctAnswerTag.Length));
                }
                if (parameters[parameter].Contains(wrongAnswerTag))
                {
                    wrong.Add(parameters[parameter].Remove(parameters[parameter].IndexOf(wrongAnswerTag), wrongAnswerTag.Length));
                }
            }
            return new Question(questionText, correct.ToArray(), wrong.ToArray());
        }

        private void RemoveTableFromHeather()
        {
            int answerTableStartIndex = heather.LastIndexOf(tableStartTag);
            heather.Remove(answerTableStartIndex);
        }
    }
}
